package com.jinlejia.bbs.logic;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.adapter.reply.ReplyBean;
import com.jinlejia.bbs.bean.GambitBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.http.entity.GambitEntity;
import com.jinlejia.bbs.http.entity.TopicDetailEntity;
import com.jinlejia.bbs.http.entity.UserEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;
/**
 * @author 王进 on 2019/3/28.
 */

public class ReplyDetailLogic {
    private final ReplyBean bean;
    private final Context context;
    private final LogicCallBack callBack;
    private final String topicCode;
    public ReplyDetailLogic(Context context,String topicCode,ReplyBean bean,LogicCallBack callBack){
        this.bean = bean;
        this.topicCode = topicCode;
        this.context = context;
        this.callBack = callBack;
    }

    private int pageIndex;
    private int readCount = 0;
    public void refresh(){
        pageIndex = 1;
        if (readCount==0){
//            read(topicCode);
            readCount ++ ;
        }
        loadData(topicCode,pageIndex);
    }

    public void load(){
        pageIndex = pageIndex+1;
        loadData(topicCode,pageIndex);
    }

    public void read(String topicCode){
        JSONObject json = new JSONObject();
        json.put("topicCode",topicCode);
        httpCallForm(BBSConfig.saveTopicReview,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        readCount = 0;
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        readCount = 1;
                    }

                    @Override
                    public void onNoResponse(int code) {
                        readCount = 0;
                    }
                }));
    }

    private void loadData(String topicCode,int pageIndex){
        JSONObject json = new JSONObject();
        json.put("topicCode",topicCode);
        json.put("pageIndex",pageIndex);
        json.put("pageSize",10);
        httpCallForm(BBSConfig.getTopicDetail,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false,false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        TopicDetailEntity response = parse.parse(TopicDetailEntity.class);
                        //组装话题
                        TopicBean topic = new TopicBean();
                        String _topicCode = response.getTopicCode(); //话题编码
                        topic.setTopicCode(_topicCode);
                        String _topicTitle = response.getTopicTitle(); //话题标题
                        topic.setTopicTitle(_topicTitle);
                        int _replyCount = response.getAnswerNum(); //回复次数
                        topic.setReplyCount(_replyCount);
                        int _reviewCount = response.getReviewNum(); //浏览次数
                        topic.setReviewCount(_reviewCount);
                        int _isCollection = response.getIsCollection();
                        topic.setCollection(_isCollection==1); //收藏状态
                        int _isUser = response.getIsUser();
                        topic.setOwn(_isUser==1); //是否是本人
                        //region 回答列表
                        List<GambitEntity> answerList = response.getTopicDetailAnswerResultList();
                        List<InformationBean> answerInfoList = new ArrayList<>();
                        if (answerList!=null)
                        for (GambitEntity data:answerList) {
                            if (data.getData()!=null)
                            for (GambitEntity entity:data.getData()) {
                                InformationBean bean = new InformationBean();
                                BBSUserBean user = new BBSUserBean();
                                user.setUserPin(entity.getUserPin());
                                user.setUserName(entity.getUserName());
                                user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                                if (!TextUtils.isEmpty(entity.getStoreName())){
                                    user.setUserAddress(entity.getStoreName()); ////回答用户职务
                                }else {
                                    user.setUserAddress(entity.getUserTitleLevel());
                                }
                                user.setUserDuty(entity.getUserTitleLevel()); //回答用户职务
                                int isAttention = entity.getIsAttention();
                                if (isAttention==1){
                                    user.setAttentionType(1);
                                }else if (isAttention==2){
                                    user.setAttentionType(0);
                                }else if (isAttention==3){
                                    user.setAttentionType(-1);
                                }
                                bean.setUser(user);
                                bean.setAnswerCode(entity.getAnswerCode());
                                int contentType = entity.getContentType();
                                if (contentType==1){ //设置消息体
                                    bean.setMessageContent(entity.getContent());
                                }else if (contentType==2){
                                    bean.setSoundContent(SoundBean.create(entity.getContent()));
                                }
                                bean.setReplyCount(entity.getComments()); //评论次数
                                bean.setLikeCount(entity.getSupportCount()); //点赞次数
                                bean.setOwnLike(entity.getIsSupport()==1); //自己是否点赞
                                bean.setOwn(entity.getIsUser()==1); //是否是自己的回答
                                answerInfoList.add(bean);
                            }
                        }
                        //endregion

                        //region 推荐话题列表
                        GambitEntity topicDetailRecommendResults = response.getTopicDetailRecommendResults();
                        List<GambitEntity> gambitEntityList = null;
                        boolean hasNextPage = false;
                        if (topicDetailRecommendResults!=null){
                            gambitEntityList = topicDetailRecommendResults.getData();
                            hasNextPage = topicDetailRecommendResults.getNextPageAvailable();
                        }
                        ArrayList<GambitBean> gambitList = new ArrayList<>();
                        if (gambitEntityList!=null)
                        for (GambitEntity entity : gambitEntityList) {
                            GambitBean bean = new GambitBean();
                            //组装 话题
                            TopicBean _topic = new TopicBean();
                            String topicTitle = entity.getTopicTitle();
                            _topic.setTopicTitle(topicTitle);
                            _topic.setTopicCode(entity.getTopicCode());
                            _topic.setReviewCount(entity.getReviewNum());
                            bean.setTopic(_topic);

                            InformationBean info = null;
                            if (!TextUtils.isEmpty(entity.getContent()) || !TextUtils.isEmpty(entity.getContent())){
                                info = new InformationBean();
                                if (entity.getContentType()==1){
                                    info.setMessageContent(entity.getContent()); //回答内容
                                }else if (entity.getContentType()==2){
                                    info.setSoundContent(SoundBean.create(entity.getContent())); //回答内容
                                }
                                info.setAnswerCode(entity.getAnswerCode());
                                info.setReviewCount(entity.getReviewNum()); //回答阅读次数
                                info.setLikeCount(entity.getSupportCount()); //回答点赞次数
                                info.setOwnLike(entity.getIsUser() == 1); //是否自己已经点赞

                                //region 组装回答用户
                                BBSUserBean user = new BBSUserBean();
                                user.setUserPin(entity.getUserPin()); //回答用户PIN
                                user.setUserName(entity.getUserName());//回答用户姓名
                                user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl())); //回答用户头像
                                if (!TextUtils.isEmpty(entity.getAnswerStoreName())){
                                    user.setUserAddress(entity.getAnswerStoreName()); //回答用户门店
                                }else {
                                    user.setUserAddress(entity.getUserTitleLevel());
                                }
                                info.setUser(user);
                                //endregion
                                List<UserEntity> recordsUserResults = entity.getAttitudesRecordsUserResults();
                                List<BBSUserBean> likeList = new ArrayList<>();
                                if (recordsUserResults!=null){
                                    for (UserEntity userEntity:recordsUserResults) {
                                        String imageUrl = userEntity.getImageUrl();
                                        String pin = userEntity.getPin();
                                        BBSUserBean likeUser = new BBSUserBean();
                                        likeUser.setUserAvatar(BBSConfig.headerUrl(imageUrl));
                                        likeUser.setUserPin(pin);
                                        likeList.add(likeUser);
                                    }
                                }
                                info.setOwnLike(entity.getIsUser()==1);
                                info.setLikeUserList(likeList);
                            }
                            bean.setInfo(info);
                            gambitList.add(bean);
                        }
                        //endregion
                        bean.setInformationList(answerInfoList);
                        bean.setGambitBeanList(gambitList);
                        bean.setTopic(topic);
                        callBack.onResult(hasNextPage,false);
                    }

                    @Override
                    public void onNoResponse(int code) {
//                        callBack.onResult(false);
                    }

                    @Override
                    public boolean onProxyMessage(Map<String, Object> response) {
                        if ("EC00003".equalsIgnoreCase(String.valueOf(response.get("code")))){
                            callBack.onResult(false,true);
                        }else {
                            callBack.onResult(false,false);
                        }
                        return true;
                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(boolean hasNextPage,boolean isDel);
    }
}
